import * as Cesium from "cesium";
import * as THREE from 'three';
import image1 from "@/assets/threeJs/images/camera1.png"
import image2 from "@/assets/threeJs/images/camera2.png"
import {degreeModel} from "@/threeJs/loadModel";
import monitoringData from "@/pointsDatas/monitoring.json";
import cameraIndexCode from "@/pointsDatas/cameraIndexCode.json";

let newArr = []
monitoringData.forEach((item, index) => {
    cameraIndexCode.forEach((item1) => {
        if (item.deviceName === item1.cameraName) {
            let obj = item
            obj.cameraIndexCode = item1.cameraIndexCode
            newArr.push(obj)
        }
    })

})
console.log(newArr)

function addMonitoring(cesiumViewer) {
    monitoringData.forEach((item, index) => {
        let j = 0
        let k = 0
        let multiple = 0.00005
        if (index % 5 === 1) {
            j = -1
        } else if (index % 5 === 2) {
            j = 1
        } else if (index % 5 === 3) {
            k = -1
        } else if (index % 5 === 4) {
            k = 1
        }
        // let map = textureLoader.load(item.imgType)
        // // 3. 创建精灵材质
        // const spriteMaterial = new THREE.SpriteMaterial({
        //     map: map,
        // });
        // // 4. 创建精灵并调整属性
        // const sprite = new THREE.Sprite(spriteMaterial);
        // degreeModel(sprite, item.longitude + j * multiple, item.latitude + k * multiple, 3) // 减小向左 减小向下
        // sprite.name = '精灵图标监控'
        // sprite.scale.set(10, 10, 10) // 设置精灵的大小
        // cesiumViewer.add(sprite);
        let en = cesiumViewer.entities.add({
            cameraIndexCode: item.cameraIndexCode ? item.cameraIndexCode : 'fe08153bf5d141d38a37ba415829501c',
            parkName: item.parkName,
            show: false,
            id: item.deviceName,
            name: '监控图标',
            position: Cesium.Cartesian3.fromDegrees(item.longitude + j * multiple, item.latitude + k * multiple, 10),
            billboard: {
                // 设置广告牌的显示位置
                verticalOrigin: Cesium.VerticalOrigin.TOP,
                // 设置广告牌的显示位置
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                image: image1,
                width: 40,
                height: 40,
                // // 控制最小和最大像素尺寸
                // minimumPixelSize: 25,  // 最小显示尺寸(像素)
                // maximumScale: 100,     // 最大缩放比例
                // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                //     0,    // 最小可见距离(米)
                //     2000 // 最大可见距离(米)
                // )
            },
        });
    })
}

function displayMonitoring(currentPark, viewer, flag) {
    if (flag) {
        viewer.entities.values.forEach((item) => {
            if (item.name === '监控图标') {
                if (item.parkName === currentPark) {
                    item.show = true
                } else {
                    item.show = false
                }
            }
        })
    } else {
        if (targetEntity) targetEntity.billboard.image = image1;
        viewer.entities.values.forEach((item) => {
            if (item.name === '监控图标') {
                item.show = false
            }
        })
    }

}

let targetEntity = null

function monitoringViewPositioning(viewer, name) {
    if (targetEntity) targetEntity.billboard.image = image1;
    targetEntity = viewer.entities.getById(name)
    // 更换广告牌图片
    if (targetEntity && targetEntity.billboard) {
        targetEntity.billboard.image = image2;  // 新照片路径
    }
    viewer.flyTo(targetEntity,
        {
            duration: 2, // 飞行持续时间（秒）
            maximumHeight: 1000, // 飞行过程中的最大高度（米）
            offset: {
                heading: Cesium.Math.toRadians(0), // 航向角（弧度）
                pitch: Cesium.Math.toRadians(-90), // 俯仰角（弧度），-90表示从正上方看
                range: 200 // 与实体的距离（米）
            }
        }
    );
}

export {addMonitoring, displayMonitoring, monitoringViewPositioning}
